package com.bcsp.xml;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;

public class Test6 {
    public static void main(String[] args) throws Exception{
        //使用dom4j
        //获取文件路径
        String path = Test6.class.getClassLoader().getResource("books.xml").getPath();
        //准备SAXReader
        SAXReader reader = new SAXReader();
        //读取文档
        Document document = reader.read(new File(path));
        //获取根节点
        Element root = document.getRootElement();
        for (Element book : root.elements("book")){
            String title = book.elementText("title");
            String lang = book.element("title").attributeValue("lang");
            //String author = book.elementText("author");
            for(Element authorElement : book.elements("author")){
                String author = authorElement.getText();
                System.out.println("\t作者:" + author);
            }
            String year = book.elementText("year");
            String price = book.elementText("price");
            System.out.println("标题:<<" + title +">> 语言:" + lang);
            //System.out.println("\t作者:" + author);
            System.out.println("\t出版日期:" + year);
            System.out.println("\t售价:" + price);
        }
    }
}
